package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.platform.dto.BasicDeviceFaultRecordDTO;
import com.iwomy.secureplat.platform.entity.BasicDeviceFaultRecordEntity;
import com.iwomy.secureplat.platform.service.BasicDeviceFaultRecordService;
import com.iwomy.secureplat.platform.service.BasicInstallLocationService;
import com.iwomy.secureplat.platform.vo.BasicDeviceFaultRecordVO;
import com.iwomy.secureplat.platform.vo.BasicInstallLocationVO;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 故障记录表
 *
 * @author pig
 * @date 2023-11-23 10:49:50
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicDeviceFaultRecord")
@Tag(description = "basicDeviceFaultRecord", name = "故障记录表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicDeviceFaultRecordController {

	private final BasicDeviceFaultRecordService basicDeviceFaultRecordService;

	private final BasicInstallLocationService basicInstallLocationService;

	/**
	 * 分页查询
	 *
	 * @param page                      分页对象
	 * @param basicDeviceFaultRecordDTO 故障记录表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_basicDeviceFaultRecord_view')")
	public R getBasicDeviceFaultRecordPage(@ParameterObject Page page, @ParameterObject BasicDeviceFaultRecordDTO basicDeviceFaultRecordDTO) {
		Page<BasicDeviceFaultRecordVO> deviceFaultRecordVO = basicDeviceFaultRecordService.getBasicDeviceFaultRecordPage(page, basicDeviceFaultRecordDTO);

		List<BasicDeviceFaultRecordVO> records = deviceFaultRecordVO.getRecords();
		for (BasicDeviceFaultRecordVO record : records) {
			Long installLocationId = record.getInstallLocationId();
			if (installLocationId != null && installLocationId != 0) {
				BasicInstallLocationVO basicInstallLocation = basicInstallLocationService.getByIds(installLocationId);
				record.setInstallAddress(basicInstallLocation.getAddress());
			}
		}
		return R.ok(deviceFaultRecordVO);
	}

	/**
	 * 分页查询
	 *
	 * @param page                      分页对象
	 * @param basicDeviceFaultRecordDTO imei历史故障记录表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/getImeiList")
	@PreAuthorize("@pms.hasPermission('platform_basicDeviceFaultRecord_view')")
	public R getImeiList(@ParameterObject Page page, @ParameterObject BasicDeviceFaultRecordDTO basicDeviceFaultRecordDTO) {
		return R.ok(basicDeviceFaultRecordService.getImeiList(page, basicDeviceFaultRecordDTO));
	}

	/**
	 * 分页查询
	 *
	 * @param basicDeviceFaultRecord imei历史故障记录表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/list")
	@PreAuthorize("@pms.hasPermission('platform_basicDeviceFaultRecord_view')")
	public R getList(@ParameterObject BasicDeviceFaultRecordEntity basicDeviceFaultRecord) {
		LambdaQueryWrapper<BasicDeviceFaultRecordEntity> wrapper = Wrappers.lambdaQuery(basicDeviceFaultRecord);
		return R.ok(basicDeviceFaultRecordService.list(wrapper));
	}

	/**
	 * 通过id查询故障记录表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_basicDeviceFaultRecord_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(basicDeviceFaultRecordService.getById(id));
	}

	/**
	 * 新增故障记录表
	 *
	 * @param basicDeviceFaultRecord 故障记录表
	 * @return R
	 */
	@Operation(summary = "新增故障记录表", description = "新增故障记录表")
	@SysLog("新增故障记录表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_basicDeviceFaultRecord_add')")
	public R save(@RequestBody BasicDeviceFaultRecordEntity basicDeviceFaultRecord) {
		return R.ok(basicDeviceFaultRecordService.save(basicDeviceFaultRecord));
	}

	/**
	 * 修改故障记录表
	 *
	 * @param basicDeviceFaultRecord 故障记录表
	 * @return R
	 */
	@Operation(summary = "修改故障记录表", description = "修改故障记录表")
	@SysLog("修改故障记录表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_basicDeviceFaultRecord_edit')")
	public R updateById(@RequestBody BasicDeviceFaultRecordEntity basicDeviceFaultRecord) {
		return R.ok(basicDeviceFaultRecordService.updateById(basicDeviceFaultRecord));
	}

	/**
	 * 通过id删除故障记录表
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除故障记录表", description = "通过id删除故障记录表")
	@SysLog("通过id删除故障记录表")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_basicDeviceFaultRecord_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(basicDeviceFaultRecordService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param basicDeviceFaultRecord 查询条件
	 * @param ids                    导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_basicDeviceFaultRecord_export')")
	public List<BasicDeviceFaultRecordEntity> export(BasicDeviceFaultRecordEntity basicDeviceFaultRecord, Long[] ids) {
		return basicDeviceFaultRecordService.list(Wrappers.lambdaQuery(basicDeviceFaultRecord).in(ArrayUtil.isNotEmpty(ids), BasicDeviceFaultRecordEntity::getId, ids));
	}
}